package com.limingru.newbee.api;

import com.limingru.newbee.api.param.*;
import com.limingru.newbee.api.vo.UserVO;
import com.limingru.newbee.common.Constants;
import com.limingru.newbee.common.ServiceResultEnum;
import com.limingru.newbee.entity.User;
import com.limingru.newbee.service.UserService;
import com.limingru.newbee.util.NumberUtil;
import com.limingru.newbee.util.ResultGenerator;
import com.limingru.newbee.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@Api(value = "v1", tags = "新蜂商城用户操作相关接口")
@RequestMapping("/api/v1")
public class UserAPI {

    @Resource
    private UserService userService;

    @PostMapping("/user/login")
    @ApiOperation(value = "登录接口", notes = "校验用户登录，如果成功返回token")
    public Result<String> login(@RequestBody @Valid MallUserLoginParam mallUserLoginParam) {
        if (!NumberUtil.isPhone(mallUserLoginParam.getLoginName())) {
            return ResultGenerator.generateFailResult("请输入正确的手机号");
        }
        String loginResult = userService.login(mallUserLoginParam.getLoginName(), mallUserLoginParam.getPasswordMD5());

        if (!StringUtils.isEmpty(loginResult) && loginResult.length() == Constants.TOKEN_LENGTH) {
            Result result = ResultGenerator.generateSuccessResult();
            result.setData(loginResult);
            return result;
        }

        return ResultGenerator.generateFailResult("用户名或密码错误");
    }

    @PostMapping("/user/register")
    public Result<String> register(@RequestBody @Valid MallUserRegisterParam mallUserRegisterParam) {
        if (!NumberUtil.isPhone(mallUserRegisterParam.getLoginName())) {
            return ResultGenerator.generateFailResult(ServiceResultEnum.LOGIN_NAME_IS_NOT_PHONE.getText());
        }
        String registerResult = userService.register(mallUserRegisterParam.getLoginName(), mallUserRegisterParam.getPassword());

        if (ServiceResultEnum.REGISTER_SUCCESS.getText().equals(registerResult)) {
            return ResultGenerator.generateSuccessResult(registerResult);
        }
        return ResultGenerator.generateFailResult(registerResult);
    }

    @PostMapping("/user/logout")
    @ApiOperation(value = "退出接口", notes = "用户退出")
    public Result<String> logout(@RequestBody @Valid MallUserLogoutParam mallUserLogoutParam) {
        boolean logout = userService.logout(mallUserLogoutParam.getUserId());
        return logout ? ResultGenerator.generateSuccessResult("退出成功") : ResultGenerator.generateFailResult("退出失败");
    }

    @PostMapping("/user/info")
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    public Result<UserVO> getUserInfo(@RequestBody @Valid MallUserGetUserInfoParam getUserInfoParam) {
        UserVO userVO =  userService.getUserInfo(getUserInfoParam.getToken());
        if (userVO != null) {
            return ResultGenerator.generateSuccessResult(userVO);
        }
        Result result = new Result();
        result.setResultCode(416);
        result.setMessage("登录态失效或用户不存在");
        return result;
    }

    @PostMapping("/user/update")
    public Result<String> updateUserInfo(@RequestBody @Valid MallUserUpdateParam userUpdateParam) {
        String updateResult = userService.update(userUpdateParam);
        if (ServiceResultEnum.UPDATE_SUCCESS.getText().equals(updateResult)) {
            return ResultGenerator.generateSuccessResult(updateResult);
        }
        return ResultGenerator.generateFailResult(updateResult);
    }
}
